package com.ruoyi.contract.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.contract.domain.Contract;
import com.ruoyi.contract.domain.ContractProjectConnection;
import com.ruoyi.contract.domain.ContractSigningPartyConnection;
import com.ruoyi.contract.domain.contractVo.*;
import com.ruoyi.contract.mapper.ContractMapper;
import com.ruoyi.contract.mapper.ContractProjectConnectionMapper;
import com.ruoyi.contract.mapper.ContractSigningPartyConnectionMapper;
import com.ruoyi.contract.service.IContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.contract.mapper.ContractProjectMapper;
import com.ruoyi.contract.domain.ContractProject;
import com.ruoyi.contract.service.IContractProjectService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-18
 */
@Service
public class ContractProjectServiceImpl implements IContractProjectService 
{
    @Autowired
    private ContractProjectMapper contractProjectMapper;

    @Autowired
    private ContractSigningPartyConnectionMapper contractSigningPartyConnectionMapper;

    @Autowired
    private ContractProjectConnectionMapper contractProjectConnectionMapper;

    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private IContractService contractService;

    /**
     * 查询项目
     * 
     * @param contractProjectId 项目主键
     * @return 项目
     */
    @Override
    public ProjectAllContractVo selectContractProjectByContractProjectId(Long contractProjectId)
    {
        // 根据项目id获取项目信息
        ContractProject contractProject = contractProjectMapper.selectContractProjectByContractProjectId(contractProjectId);
        ProjectAllContractVo projectAllContractVo = new ProjectAllContractVo(contractProject);

// 在项目合同连表中，根据项目id查出对应合同id的List列
        ContractProjectConnection contractProjectConnection = new ContractProjectConnection();
        contractProjectConnection.setContractProjectId(contractProjectId);
        List<ContractProjectConnection> contractProjectConnections = contractProjectConnectionMapper.selectContractProjectConnectionList(contractProjectConnection);
        List<Long> contractIds = new ArrayList<>();
        for (ContractProjectConnection contractProjectConnection1 : contractProjectConnections) {
            if (contractProjectConnection1.getContractId() != null) {
                contractIds.add(contractProjectConnection1.getContractId());
            }
        }

// 使用合同id的List列查出合同的详细信息并分类，并在合同签约方连表中查出对应签约方的name
        for (Long contractId : contractIds) {
            if (contractId == null) continue; // 跳过空的合同ID

            // 根据合同id查出对应合同信息
            Contract contract = contractMapper.selectContractByContractId(contractId);
            if (contract == null) {
                System.out.println("合同不存在，合同ID: " + contractId);
                continue; // 如果合同不存在，跳过
            }

            // 根据合同id查出对应签约方信息，并保存所有对应签约方name到List中
            ContractSigningPartyConnection contractSigningPartyConnection = new ContractSigningPartyConnection();
            contractSigningPartyConnection.setContractId(contractId);
                //合同与签约方连表
            List<ContractSigningPartyConnection> contractSigningPartyConnections = contractSigningPartyConnectionMapper.selectContractSigningPartyConnectionList(contractSigningPartyConnection);
            List<String> contractPartyNameList = new ArrayList<>();//签约方List
            for (ContractSigningPartyConnection contractSigningPartyConnection1 : contractSigningPartyConnections) {
                if (contractSigningPartyConnection1.getContractPartyName() != null) {
                    contractPartyNameList.add(contractSigningPartyConnection1.getContractPartyName());
                }
            }

            // 根据合同类型，分为进项和销项
            if ("进项合同".equals(contract.getType())) {
                ContractAllPartyVo incomeContractList = new ContractAllPartyVo(contract);
                incomeContractList.setContractPartyNameList(contractPartyNameList);
                projectAllContractVo.getIncomeContractList().add(incomeContractList);
            } else if("销项合同".equals(contract.getType())){
                ContractAllPartyVo outputContractList = new ContractAllPartyVo(contract);
                outputContractList.setContractPartyNameList(contractPartyNameList);
                projectAllContractVo.getOutputContractList().add(outputContractList);
            }
        }
//        return contractProjectMapper.selectContractProjectByContractProjectId(contractProjectId);
        return projectAllContractVo;
    }

    /**
     * 查询项目列表
     * 
     * @param contractProject 项目
     * @return 项目
     */
    @Override
    public List<ContractProject> selectContractProjectList(ContractProject contractProject)
    {
        return contractProjectMapper.selectContractProjectList(contractProject);
    }

    /**
     * 新增项目
     * 
     * @param rewriteProjectContractVo 项目
     * @return 结果
     */
    @Override
    @Transactional
    public int insertContractProject(RewriteProjectContractVo rewriteProjectContractVo)
    {
        //新增项目
        ContractProject contractProject = rewriteProjectContractVo.getContractProject();
        contractProject.setAddUserId(SecurityUtils.getUserId());
        contractProject.setAddTime(DateUtils.getNowDate());
        int insert =contractProjectMapper.insertContractProject(contractProject);

        //新增项目合同连表
        for(Long contractId : rewriteProjectContractVo.getContractIds()) {
            ContractProjectConnection contractProjectConnection  = new ContractProjectConnection();
            contractProjectConnection.setContractProjectId(contractProject.getContractProjectId());
            contractProjectConnection.setContractId(contractId);
            contractProjectConnectionMapper.insertContractProjectConnection(contractProjectConnection);
        }

        return insert;
    }

    /**
     * 修改项目
     * 
     * @param rewriteProjectContractVo 项目
     * @return 结果
     */
    @Override
    @Transactional
    public int updateContractProject(RewriteProjectContractVo rewriteProjectContractVo)
    {
        //修改项目信息
        ContractProject contractProject = rewriteProjectContractVo.getContractProject();
        contractProject.setUpdateUserId(SecurityUtils.getUserId());
        contractProject.setUpdateTime(DateUtils.getNowDate());

        //删除所有此项目，项目合同连表中相关内容
        contractProjectConnectionMapper.deleteContractProjectConnectionByContractProjectId(contractProject.getContractProjectId());

        //根据传入合同idList重新添加此项目，项目合同连表中相关内容
        for(Long contractId : rewriteProjectContractVo.getContractIds()) {
            ContractProjectConnection contractProjectConnection  = new ContractProjectConnection();
            contractProjectConnection.setContractProjectId(contractProject.getContractProjectId());
            contractProjectConnection.setContractId(contractId);
            contractProjectConnectionMapper.insertContractProjectConnection(contractProjectConnection);
        }

        return contractProjectMapper.updateContractProject(contractProject);
    }

    /**
     * 批量删除项目
     * 
     * @param contractProjectIds 需要删除的项目主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteContractProjectByContractProjectIds(Long[] contractProjectIds)
    {
        for(Long contractProjectId : contractProjectIds){
            //删除此项目相关项目合同连表
            contractProjectConnectionMapper.deleteContractProjectConnectionByContractProjectId(contractProjectId);
        }
        return contractProjectMapper.deleteContractProjectByContractProjectIds(contractProjectIds);
    }

    /**
     * 删除项目信息
     * 
     * @param contractProjectId 项目主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteContractProjectByContractProjectId(Long contractProjectId)
    {
        //删除此项目相关项目合同连表
        contractProjectConnectionMapper.deleteContractProjectConnectionByContractProjectId(contractProjectId);
        return contractProjectMapper.deleteContractProjectByContractProjectId(contractProjectId);
    }

    /**
     * 查询项目对应所属公司未选择过的合同
     *
     * @param selectedContracts 项目主键
     * @return 结果
     */
    @Override
    public List<ContractAllPartyVo> unselectedContracts(SelectedContractsVo selectedContracts) {
        List<ContractAllPartyVo> contractAllPartyVos= new ArrayList<>();

        Contract contract = new Contract();
        contract.setBelongingCompanyId(selectedContracts.getBelongingCompanyId());
        contract.setType(selectedContracts.getType());
        contract.setName(selectedContracts.getName());
        contract.setBelongingCompanyName(selectedContracts.getBelongingCompanyName());
        contract.setContractCategoryId(selectedContracts.getContractCategoryId());
        contract.setPartyString(selectedContracts.getPartyString());
        List<Contract> contracts = contractMapper.selectContractList(contract);
        //使用流式API过滤掉已存在的合同
        contracts = contracts.stream()
                .filter(c -> !selectedContracts.getContractIds().contains(c.getContractId()))
                .collect(Collectors.toList());

        for (Contract contract1:contracts){
            ContractAllPartyVo contractAllPartyVo = new ContractAllPartyVo(contract1);

            // 根据合同id查出对应签约方信息，并保存所有对应签约方name到List中
            ContractSigningPartyConnection contractSigningPartyConnection = new ContractSigningPartyConnection();
            contractSigningPartyConnection.setContractId(contract1.getContractId());
            //合同与签约方连表
            List<ContractSigningPartyConnection> contractSigningPartyConnections = contractSigningPartyConnectionMapper.selectContractSigningPartyConnectionList(contractSigningPartyConnection);
            List<String> contractPartyNameList = new ArrayList<>();//签约方List
            for (ContractSigningPartyConnection contractSigningPartyConnection1 : contractSigningPartyConnections) {
                if (contractSigningPartyConnection1.getContractPartyName() != null) {
                    contractPartyNameList.add(contractSigningPartyConnection1.getContractPartyName());
                }
            }
            contractAllPartyVo.setContractPartyNameList(contractPartyNameList);
            contractAllPartyVos.add(contractAllPartyVo);
        }
        return contractAllPartyVos;
    }
}
